/*
 * IntMatrix.cpp
 *
 *  Created on: Feb 28, 2011
 *      Author: santiago
 */

#include <vector>
#include <assert.h>

#include "IntMatrix.h"

using namespace std;

IntMatrix::IntMatrix(unsigned int size): _size(size) {
	_matrix.reserve(size);

	vector<int> *rowMatrix;
	for (unsigned int nodeIndex = 0; nodeIndex < size; nodeIndex++) {
		rowMatrix = new vector<int>();
		rowMatrix->reserve(nodeIndex+1);

		for (unsigned int destiantionNodeIndex = 0;
				destiantionNodeIndex < nodeIndex + 1; destiantionNodeIndex++) {

			rowMatrix->push_back(0);
		}

		_matrix.push_back(*rowMatrix);
		delete rowMatrix;
	}
}

IntMatrix::~IntMatrix() {
	// TODO Auto-generated destructor stub
}

int IntMatrix::get(unsigned int i, unsigned int j) {
	unsigned int first_index;
	unsigned int second_index;
	convert_to_matrix(i, j, first_index, second_index);

	return _matrix[first_index][second_index];
}

void IntMatrix::set(unsigned int i, unsigned int j, int value) {
	unsigned int first_index;
	unsigned int second_index;
	convert_to_matrix(i, j, first_index, second_index);

	_matrix[first_index][second_index] = value;
}

void IntMatrix::convert_to_matrix(unsigned int i, unsigned int j,
		unsigned int &first_index, unsigned int &second_index) const {
	if (i < j) {
		first_index = j;
		second_index = i;
	} else {
		first_index = i;
		second_index = j;
	}

	assert(_matrix.size() > first_index);
	assert(_matrix[first_index].size() > second_index);
}

int IntMatrix::size() const {
	return _size;
}

